home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyo (Python 2.5) from __future__ import generators import sys import warnings import os import fnmatch import glob import shutil import codecs import hashlib from os.path import join as pathjoin __version__ = '2.2' __all__ = [ 'path'] if os.name == 'nt': try: import win32security except ImportError: win32security = None except: None<EXCEPTION MATCH>ImportError None<EXCEPTION MATCH>ImportError try: import pwd except ImportError: pwd = None _base = str _getcwd = os.getcwd try: if os.path.supports_unicode_filenames: _base = unicode _getcwd = os.getcwdu _filesystem_encoding = sys.getfilesystemencoding() except AttributeError: pass try: (True, False) except NameError: (True, False) = (1, 0) try: basestring except NameError: basestring = (str, unicode) _textmode = 'r' if hasattr(file, 'newlines'): _textmode = 'U' class TreeWalkWarning(Warning): pass class path(_base): def __repr__(self): return 'path(%s)' % _base.__repr__(self) def __add__(self, more): try: resultStr = _base.__add__(self, more) except TypeError: resultStr = NotImplemented if resultStr is NotImplemented: return resultStr return self.__class__(resultStr) def __radd__(self, other): if isinstance(other, basestring): return self.__class__(other.__add__(self)) else: return NotImplemented def __div__(self, rel): return self.__class__(pathjoin(self, rel)) __truediv__ = __div__ def getcwd(cls): return cls(_getcwd()) getcwd = classmethod(getcwd) isabs = os.path.isabs def url(self): return self.__class__('file:///' + self.abspath().replace('\\', '/')) def abspath(self): return self.__class__(os.path.abspath(self)) def normcase(self): return self.__class__(os.path.normcase(self)) def normpath(self): return self.__class__(os.path.normpath(self)) def realpath(self): return self.__class__(os.path.realpath(self)) def expanduser(self): return self.__class__(os.path.expanduser(self)) def expandvars(self): return self.__class__(os.path.expandvars(self)) def dirname(self): return self.__class__(os.path.dirname(self)) basename = os.path.basename def expand(self): return self.expandvars().expanduser().normpath().normcase() def _get_namebase(self): (base, ext) = os.path.splitext(self.name) return base def _get_ext(self): (f, ext) = os.path.splitext(_base(self)) return ext def _get_drive(self): (drive, r) = os.path.splitdrive(self) return self.__class__(drive) parent = property(dirname, None, None, " This path's parent directory, as a new path object.\n\n For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib')\n ") name = property(basename, None, None, " The name of this file or directory without the full path.\n\n For example, path('/usr/local/lib/libpython.so').name == 'libpython.so'\n ") namebase = property(_get_namebase, None, None, " The same as path.name, but with one file extension stripped off.\n\n For example, path('/home/guido/python.tar.gz').name == 'python.tar.gz',\n but path('/home/guido/python.tar.gz').namebase == 'python.tar'\n ") ext = property(_get_ext, None, None, " The file extension, for example '.py'. ") drive = property(_get_drive, None, None, " The drive specifier, for example 'C:'.\n This is always empty on systems that don't use drive specifiers.\n ") def splitpath(self): (parent, child) = os.path.split(self) return (self.__class__(parent), child) def splitdrive(self): (drive, rel) = os.path.splitdrive(self) return (self.__class__(drive), rel) def splitext(self): (filename, ext) = os.path.splitext(self) return (self.__class__(filename), ext) def stripext(self): return self.splitext()[0] if hasattr(os.path, 'splitunc'): def splitunc(self): (unc, rest) = os.path.splitunc(self) return (self.__class__(unc), rest) def _get_uncshare(self): (unc, r) = os.path.splitunc(self) return self.__class__(unc) uncshare = property(_get_uncshare, None, None, ' The UNC mount point for this path.\n This is empty for paths on local drives. ') def joinpath(self, *args): return self.__class__(pathjoin(self, *args)) def splitall(self): parts = [] loc = self while loc != os.curdir and loc != os.pardir: prev = loc (loc, child) = prev.splitpath() if loc == prev: break parts.append(child) parts.append(loc) parts.reverse() return parts def relpath(self): cwd = self.__class__(os.getcwd()) return cwd.relpathto(self) def relpathto(self, dest): origin = self.abspath() dest = self.__class__(dest).abspath() orig_list = origin.normcase().splitall() dest_list = dest.splitall() if orig_list[0] != os.path.normcase(dest_list[0]): return dest i = 0 for start_seg, dest_seg in zip(orig_list, dest_list): if start_seg != os.path.normcase(dest_seg): break i += 1 segments = [ os.pardir] * (len(orig_list) - i) segments += dest_list[i:] if len(segments) == 0: relpath = os.curdir else: relpath = pathjoin(*segments) return self.__class__(relpath) def listdir(self, pattern = None): try: names = os.listdir(self) except Exception: return [] if pattern is not None: names = fnmatch.filter(names, pattern) return [ self / child for child in names ] def dirs(self, pattern = None): return _[1] def files(self, pattern = None): return _[1] def walk(self, pattern = None, errors = 'strict'): if errors not in ('strict', 'warn', 'ignore'): raise ValueError('invalid errors parameter') try: childList = self.listdir() except Exception: if errors == 'ignore': return None elif errors == 'warn': warnings.warn("Unable to list directory '%s': %s" % (self, sys.exc_info()[1]), TreeWalkWarning) return None else: raise except: errors == 'ignore' for child in childList: if pattern is None or child.fnmatch(pattern): yield child try: isdir = child.isdir() except Exception: if errors == 'ignore': isdir = False elif errors == 'warn': warnings.warn("Unable to access '%s': %s" % (child, sys.exc_info()[1]), TreeWalkWarning) isdir = False else: raise except: errors == 'ignore' if isdir: for item in child.walk(pattern, errors): yield item def walkdirs(self, pattern = None, errors = 'strict', top_down = True): if errors not in ('strict', 'warn', 'ignore'): raise ValueError('invalid errors parameter') try: dirs = self.dirs() except Exception: if errors == 'ignore': return None elif errors == 'warn': warnings.warn("Unable to list directory '%s': %s" % (self, sys.exc_info()[1]), TreeWalkWarning) return None else: raise except: errors == 'ignore' for child in dirs: if top_down: if pattern is None or child.fnmatch(pattern): yield child for subsubdir in child.walkdirs(pattern, errors): yield subsubdir if not top_down: if pattern is None or child.fnmatch(pattern): yield child child.fnmatch(pattern) def walkfiles(self, pattern = None, errors = 'strict'): if errors not in ('strict', 'warn', 'ignore'): raise ValueError('invalid errors parameter') try: childList = self.listdir() except Exception: if errors == 'ignore': return None elif errors == 'warn': warnings.warn("Unable to list directory '%s': %s" % (self, sys.exc_info()[1]), TreeWalkWarning) return None else: raise except: errors == 'ignore' for child in childList: try: isfile = child.isfile() if not isfile: pass isdir = child.isdir() except: if errors == 'ignore': continue elif errors == 'warn': warnings.warn("Unable to access '%s': %s" % (self, sys.exc_info()[1]), TreeWalkWarning) continue else: raise if isfile: if pattern is None or child.fnmatch(pattern): yield child child.fnmatch(pattern) if isdir: for f in child.walkfiles(pattern, errors): yield f def fnmatch(self, pattern): return fnmatch.fnmatch(self.name, pattern) def glob(self, pattern): cls = self.__class__ return [ cls(s) for s in glob.glob(_base(self / pattern)) ] def open(self, mode = 'r'): return file(self, mode) def openfolder(self): if os.name == 'nt' and self.exists(): Popen = Popen import subprocess args = [ 'explorer', '/select,', '%s' % self.abspath().encode(sys.getfilesystemencoding())] Popen(args, shell = True) else: os.startfile(self.parent) def bytes(self): f = self.open('rb') try: return f.read() finally: f.close() def write_bytes(self, bytes, append = False): if append: mode = 'ab' else: mode = 'wb' f = self.open(mode) try: f.write(bytes) finally: f.close() def text(self, encoding = None, errors = 'strict'): if encoding is None: f = self.open(_textmode) try: return f.read() finally: f.close() else: f = codecs.open(self, 'r', encoding, errors) try: t = f.read() finally: f.close() return t.replace(u'\r\n', u'\n').replace(u'\r…', u'\n').replace(u'\r', u'\n').replace(u'…', u'\n').replace(u'
', u'\n') def write_text(self, text, encoding = None, errors = 'strict', linesep = os.linesep, append = False): if isinstance(text, unicode): if linesep is not None: text = text.replace(u'\r\n', u'\n').replace(u'\r…', u'\n').replace(u'\r', u'\n').replace(u'…', u'\n').replace(u'
', u'\n') text = text.replace(u'\n', linesep) if encoding is None: encoding = sys.getdefaultencoding() bytes = text.encode(encoding, errors) elif linesep is not None: text = text.replace('\r\n', '\n').replace('\r', '\n') bytes = text.replace('\n', linesep) self.write_bytes(bytes, append) def lines(self, encoding = None, errors = 'strict', retain = True): if encoding is None and retain: f = self.open(_textmode) try: return f.readlines() finally: f.close() else: return self.text(encoding, errors).splitlines(retain) def write_lines(self, lines, encoding = None, errors = 'strict', linesep = os.linesep, append = False): if append: mode = 'ab' else: mode = 'wb' f = self.open(mode) try: for line in lines: isUnicode = isinstance(line, unicode) if linesep is not None: if isUnicode: if line[-2:] in (u'\r\n', u'\r…'): line = line[:-2] elif line[-1:] in (u'\r', u'\n', u'…', u'
'): line = line[:-1] elif line[-2:] == '\r\n': line = line[:-2] elif line[-1:] in ('\r', '\n'): line = line[:-1] line += linesep if isUnicode: if encoding is None: encoding = sys.getdefaultencoding() line = line.encode(encoding, errors) f.write(line) finally: f.close() def read_md5(self): f = self.open('rb') try: m = hashlib.md5() while True: d = f.read(8192) if not d: break m.update(d) finally: f.close() return m.digest() exists = os.path.exists isdir = os.path.isdir isfile = os.path.isfile islink = os.path.islink ismount = os.path.ismount if hasattr(os.path, 'samefile'): samefile = os.path.samefile getatime = os.path.getatime atime = property(getatime, None, None, ' Last access time of the file. ') getmtime = os.path.getmtime mtime = property(getmtime, None, None, ' Last-modified time of the file. ') if hasattr(os.path, 'getctime'): getctime = os.path.getctime ctime = property(getctime, None, None, ' Creation time of the file. ') getsize = os.path.getsize size = property(getsize, None, None, ' Size of the file, in bytes. ') if hasattr(os, 'access'): def access(self, mode): return os.access(self, mode) def stat(self): return os.stat(self) def lstat(self): return os.lstat(self) def get_owner(self): if os.name == 'nt': if win32security is None: raise Exception('path.owner requires win32all to be installed') desc = win32security.GetFileSecurity(self, win32security.OWNER_SECURITY_INFORMATION) sid = desc.GetSecurityDescriptorOwner() (account, domain, typecode) = win32security.LookupAccountSid(None, sid) return domain + u'\\' + account elif pwd is None: raise NotImplementedError('path.owner is not implemented on this platform.') st = self.stat() return pwd.getpwuid(st.st_uid).pw_name owner = property(get_owner, None, None, ' Name of the owner of this file or directory. ') if hasattr(os, 'statvfs'): def statvfs(self): return os.statvfs(self) if hasattr(os, 'pathconf'): def pathconf(self, name): return os.pathconf(self, name) def utime(self, times): os.utime(self, times) def chmod(self, mode): os.chmod(self, mode) if hasattr(os, 'chown'): def chown(self, uid, gid): os.chown(self, uid, gid) def rename(self, new): os.rename(self, new) def renames(self, new): os.renames(self, new) def mkdir(self, mode = 511): os.mkdir(self, mode) def makedirs(self, mode = 511): os.makedirs(self, mode) def rmdir(self): os.rmdir(self) def removedirs(self): os.removedirs(self) def touch(self): fd = os.open(self, os.O_WRONLY | os.O_CREAT, 438) os.close(fd) os.utime(self, None) def remove(self): os.remove(self) def unlink(self): os.unlink(self) if hasattr(os, 'link'): def link(self, newpath): os.link(self, newpath) if hasattr(os, 'symlink'): def symlink(self, newlink): os.symlink(self, newlink) if hasattr(os, 'readlink'): def readlink(self): return self.__class__(os.readlink(self)) def readlinkabs(self): p = self.readlink() if p.isabs(): return p else: return (self.parent / p).abspath() copyfile = shutil.copyfile copymode = shutil.copymode copystat = shutil.copystat copy = shutil.copy copy2 = shutil.copy2 copytree = shutil.copytree if hasattr(shutil, 'move'): move = shutil.move rmtree = shutil.rmtree if hasattr(os, 'chroot'): def chroot(self): os.chroot(self) if hasattr(os, 'startfile'): def startfile(self): os.startfile(self) if _base is unicode: def __str__(self): return self.encode(_filesystem_encoding)